package com.utils.conmponentbase.base.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.launcher.ARouter;
import com.utils.conmponentbase.base.BaseModel;

/**
 * MvvmDemo com.utils.conmponentbase.base.viewmodel
 * 2020-01-11 11:04
 */
public class BaseViewModel<T extends BaseModel> extends AndroidViewModel {
    protected T mModel;
    private UIControlLiveData mUIControlLiveData;

    public BaseViewModel(@NonNull Application application) {
        super(application);
    }

    public BaseViewModel(@NonNull Application application, T pModel) {
        super(application);
        mModel = pModel;
    }

    /**
     * UI控制 LiveData
     *
     * @return {@link UIControlLiveData}
     */
    public UIControlLiveData getUILiveData() {
        if (null == mUIControlLiveData) {
            mUIControlLiveData = new UIControlLiveData();
        }
        return mUIControlLiveData;
    }

    /**
     * 展示/显示Dialog对话框
     */
    public void showDialog() {
        mUIControlLiveData.showDialogEvent.postValue("加载中...");
    }

    /**
     * 展示/显示Dialog对话框
     *
     * @param content 需要展示的内容
     */
    public void showDialog(String content) {
        mUIControlLiveData.showDialogEvent.postValue(content);
    }

    /**
     * 跳转页面
     *
     * @param routePath 目标页面
     */
    public void gotoActivity(String routePath) {
        gotoActivity(routePath, false);
    }

    public void gotoActivity(String routePath, boolean closeCurrentActivity) {
        ARouter.getInstance().build(routePath).navigation();
        if (closeCurrentActivity) mUIControlLiveData.finishEvent.call();
    }

    /**
     * 有参数值的跳转页面
     *
     * @param routePath 目标页面
     */
    public Postcard withParamValueIntent(String routePath) {
        return withParamValueIntent(routePath, false);
    }

    public Postcard withParamValueIntent(String routePath, boolean closeCurrentActivity) {
        Postcard postcard = ARouter.getInstance().build(routePath);
        if (closeCurrentActivity) mUIControlLiveData.finishEvent.call();
        return postcard;
    }

    /**
     * 关闭对话框
     */
    public void dismissDialog() {
        mUIControlLiveData.dismissDialogEvent.call();
    }

    /**
     * UI控制相关
     */
    public final class UIControlLiveData extends SingleLiveEvent {
        private SingleLiveEvent<String> showDialogEvent;
        private SingleLiveEvent<Void> dismissDialogEvent;
        private SingleLiveEvent<Void> finishEvent;
        private SingleLiveEvent<Void> onBackPressedEvent;

        public SingleLiveEvent<String> getShowDialogEvent() {
            return showDialogEvent = createLiveData(showDialogEvent);
        }

        public SingleLiveEvent<Void> getDismissDialogEvent() {
            return dismissDialogEvent = createLiveData(dismissDialogEvent);
        }

        public SingleLiveEvent<Void> getFinishEvent() {
            return finishEvent = createLiveData(finishEvent);
        }

        public SingleLiveEvent<Void> getOnBackPressedEvent() {
            return onBackPressedEvent = createLiveData(onBackPressedEvent);
        }

        /**
         * 初始化
         */
        protected <R> SingleLiveEvent<R> createLiveData(SingleLiveEvent<R> liveData) {
            if (liveData == null) {
                liveData = new SingleLiveEvent<>();
            }
            return liveData;
        }
    }
}
